"use strict";
const request = require("request");
const getBossApiError = function(detail) {
	const e = new Error("boss_api_error");
	e.additionalData = detail;
	return e;
};
const send = function(option) {
	return new Promise((success, fail) => {
		const handleServerResponse = function(body) {
			console.log("服务器返回值是", body);
			if (!body || body.status !== 0) {
				console.error("服务器没有返回json");
				fail(getBossApiError(body));
			} else if (body.status !== 0) {
				console.error("服务器返回值非0");
				fail(getBossApiError(body));
			} else {
				success(body.result);
			}
		};
		console.log("发送请求的option", option);
		request(option, (e, res, body) => {
			if (e) {
				//console.error(e);
				return fail(e);
			} else {
				handleServerResponse(body);
			}
		});
	});
};

module.exports = {
	send
};